import random
import datetime
money = 10
goods = [
    ['1','苹果',2],
    ['2','香蕉',3],
]
car = []
quan = [
    ['苹果',0.1],
    ['香蕉',0.1],
    ['苹果',0.2],
    ['香蕉',0.2],
    ['苹果',0.3],
    ['香蕉',0.3],
    ['苹果',0.4],
    ['香蕉',0.4],
    ['苹果',0.5],
    ['香蕉',0.5],
]
c = random.choice(quan)
print("恭喜你抽到优惠券：",c)
print(goods)
while True:
    choice = input('挑选商品（输入q退出）：')
    if choice == '1':
        print("有")
        if c[0] == '苹果':
            if money >= 2 * c[1]:
                print("够")
                money -= 2 * c[1]
                car.append(['苹果', 2*c[1]])
                print(money)
            else:
                print("不够")
        elif money >=2:
            print("够")
            money -=2
            car.append(['苹果',2])
            print(money)
        else:
            print("不够")

    elif choice == '2':
        if c[0] == '香蕉':
            if money >= 3 * c[1]:
                print("够")
                money -= 3 * c[1]
                car.append(['香蕉', 3*c[1]])
                print(money)
            else:
                print("不够")
        elif money >=3:
            print("够")
            money -=3
            car.append(['香蕉',3])
            print("剩余{}元".format(money))
        else:
            print("不够")
    elif choice not in ['1','2','q','3']:
        print(" 商品 '{choice}' 不存在！请重新输入")
        continue
    elif choice == 'q':
         print('购物小条')
         now: datetime=datetime.datetime.now()
         item_count = {}
         for item in car:
             if item[0] in item_count:
                 item_count[item[0]] += 1
             else:
                 item_count[item[0]] = 1
         total_value = 0
         for item_name, count in item_count.items():
            for g in goods:
                if item_name == g[1]:
                   original_price = g[2]
            if item_name == c[0]:
                actual_price =c[1] * original_price
            else:
                actual_price = original_price
            total_value += actual_price * count
         print("商品总数{}件".format(len(car)))
         print("商品总价{}元".format(total_value))
         print("卡余额{}元".format(money))
         break
    elif choice == '3':
        print("退货")
        print(car)
        s = int(input("退几号商品："))
        money += car[s - 1][1]
        car.pop(s-1)
        print('退货成功剩余{}元'.format(money))

